/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package wallythewatermelon;

import java.awt.Graphics;
import java.awt.Image;

/**
 *
 * @author mgohde
 */
public class Block extends Sprite
{
    @Override
    public void draw(Graphics g)
    {
        super.draw(g);
        if (this.wallySide() != '0') {
            DrawManager.getWally().sideHitting = this.wallySide();
        } else {
            DrawManager.getWally().blockMessageReceiver++;
        }
    }
   
    public Block(int x, int y, String imgName)
    {
        super(x, y, imgName);
    }

    public Block(int x, int y, Image img)
    {
        super(x, y, img);
    }
   
    /**
     * Provides information about where Wally is hitting a block
     * @return l=left, r=right, u=up, b=bottom, 0=hitting nowhere
     */
    public char wallySide() {
        Watermelon wally = DrawManager.getWally();
        int error=1;
        int frontx = wally.dx >= 0 ? wally.x+wally.width : wally.x;
        int fronty = wally.dy >= 0 ? wally.y+wally.height : wally.y;
        if (isBetween(frontx, this.x, this.x+this.width)) {
            if (isBetween(fronty, this.y-error, this.y+error)) {
                return 'u';
            } else if (isBetween(fronty, this.y+this.height-error, this.y+this.height+error)) {
                return 'b';
            }
        }
        if (isBetween(fronty, this.y, this.y+this.height)) {
            if (isBetween(frontx, this.x-error, this.x+error)) {
                return 'l';
            } else if (isBetween(frontx, this.x+this.width-error, this.x+this.width+error)) {
                return 'r';
            }
        }
        return '0';
    }
    
    /**
     * Checks if a is between b1 and b2
     * @param a
     * @param b1
     * @param b2
     * @return
     */
    public boolean isBetween(int a, int b1, int b2) {
        return b1<b2 ? a>=b1 && a<=b2 : a<=b1 && a>=b2;
    }
}
